/**
 * @author
 * @createtime
 * @class ArticleRegForm
 * @extends Ext.Window
 * @description ArticleReg表单
 */
ArticleRegForm = Ext.extend(Ext.Window, {
	//内嵌FormPanel
	formPanel : null,
	//构造函数
	constructor : function(_cfg) {
		Ext.applyIf(this, _cfg);
		//必须先初始化组件
		this.initUIComponents();
		ArticleRegForm.superclass.constructor.call(this, {
			id : 'ArticleRegFormWin',
			layout : 'fit',
			items : this.formPanel,
			modal : true,
			height : 500,
			width : 600,
			maximizable : true,
			title : '[收发文登记表]详细信息',
			buttonAlign : 'center',
			buttons : this.buttons,
			listeners:{
				afterrender:function(){
					Ext.getCmp('articleRegSendContainer').hide();
				}
			}
		});
	},//end of the constructor
	//初始化组件
	initUIComponents : function() {
		var thizId = this.id;
		this.formPanel = new Ext.FormPanel({
			layout : 'form',
			trackResetOnLoad : true,
			bodyStyle : 'padding:10px 10px 10px 10px',
			border : false,
			url : __ctxPath + '/produce/saveArticleReg.do',
			id : 'ArticleRegForm',
			defaults : {
				anchor : '98%,98%'
			},
			defaultType : 'textfield',
			items : [{
						name : 'articleReg.id',
						id : 'id',
						xtype : 'hidden',
						value : this.id == null ? '' : this.id
					}, {
						fieldLabel : '项目名称',
						name : 'articleRegProName',
						id : 'articleRegProName',
						allowBlank : false,
						emptyText : '请选择项目',
						listeners : {
							'focus' : function() {
								ProjectSelector.getView(function(proId, proName, proNo) {
									Ext.getCmp("articleReg.project.id").setValue(proId);
									Ext.getCmp("articleRegProName").setValue(proName);
								}, true).show();
							}
						}
					}, {
						xtype : 'hidden',
						name : 'articleReg.project.id',
						id : 'articleReg.project.id'
					}, {
						fieldLabel : '项目经理',
						name : 'articleRegManagerUser',
						id : 'articleRegManagerUser',
						readOnly : true,
						allowBlank : false,
						emptyText : '请选择项目经理',
						width : 120,
						listeners : {
							focus : function() {
								UserSelector.getView(function(ids, names) {
									Ext.getCmp("articleRegManagerUser").setValue(names);
									Ext.getCmp("articleReg.managerUser.userId").setValue(ids);
								}, true).show();
							}
						}
					}, {
						xtype : "hidden",
						name : "articleReg.managerUser.userId",
						id : "articleReg.managerUser.userId"
					}, {
						fieldLabel : '建设单位',
						name : 'articleReg.constructionUnit',
						allowBlank : false,
						maxLength : 64,
						id : 'constructionUnit'
					}, {
						fieldLabel : '监理单位',
						name : 'articleReg.supervisionUnit',
						allowBlank : false,
						maxLength : 64,
						id : 'supervisionUnit'
					}, {
						fieldLabel : '类型',
						name : 'articleReg.articleType',
						id : 'articleType',
						xtype : 'combo',
						editable:false,
						triggerAction : 'all',
						store : ['收文', '发文'],
						value:'收文',
						listeners:{
							beforeselect:function(field){
								if(!Ext.isEmpty(thizId)){
									Ext.ux.Toast.msg('操作信息', '信息修改，不能修改类型！');
									return false;
								}
							},
							select:function(field){
								if(field.getValue()=='收文'){
									Ext.getCmp('articleRegReceiveContainer').show();
									Ext.getCmp('articleRegSendContainer').hide();
									Ext.getCmp('articleReceive.unit').allowBlank=false;
									Ext.getCmp('articleReceive.articleNo').allowBlank=false;
									Ext.getCmp('articleReceive.mainContent').allowBlank=false;
									Ext.getCmp('articleReceive.receiveDate').allowBlank=false;
									Ext.getCmp('articleReceive.finishLimit').allowBlank=false;
									Ext.getCmp('articleReceiveUser').allowBlank=false;
									Ext.getCmp('articleReceiveOperator').allowBlank=false;
									Ext.getCmp('articleSend.unit').allowBlank=true;
									Ext.getCmp('articleSend.articleNo').allowBlank=true;
									Ext.getCmp('articleSend.mainContent').allowBlank=true;
									Ext.getCmp('articleSend.sendDate').allowBlank=true;
									Ext.getCmp('articleSend.num').allowBlank=true;
									Ext.getCmp('articleSignUser').allowBlank=true;
									Ext.getCmp('articleSendReceiveUser').allowBlank=true;
									Ext.getCmp('articleSend.keepAddress').allowBlank=true;
									Ext.getCmp('articleSend.receiveDate').allowBlank=true;
								}else{
									Ext.getCmp('articleRegSendContainer').show();
									Ext.getCmp('articleRegReceiveContainer').hide();
									Ext.getCmp('articleReceive.unit').allowBlank=true;
									Ext.getCmp('articleReceive.articleNo').allowBlank=true;
									Ext.getCmp('articleReceive.mainContent').allowBlank=true;
									Ext.getCmp('articleReceive.receiveDate').allowBlank=true;
									Ext.getCmp('articleReceive.finishLimit').allowBlank=true;
									Ext.getCmp('articleReceiveUser').allowBlank=true;
									Ext.getCmp('articleReceiveOperator').allowBlank=true;
									Ext.getCmp('articleSend.unit').allowBlank=false;
									Ext.getCmp('articleSend.articleNo').allowBlank=false;
									Ext.getCmp('articleSend.mainContent').allowBlank=false;
									Ext.getCmp('articleSend.sendDate').allowBlank=false;
									Ext.getCmp('articleSend.num').allowBlank=false;
									Ext.getCmp('articleSignUser').allowBlank=false;
									Ext.getCmp('articleSendReceiveUser').allowBlank=false;
									Ext.getCmp('articleSend.keepAddress').allowBlank=false;
									Ext.getCmp('articleSend.receiveDate').allowBlank=false;
								}
							}
						}
					}, {
						xtype : 'fieldset',
						title : "收文条目",
						id:'articleRegReceiveContainer',
						height : 200,
						items : [{
									xtype : 'container',
									height : 25,
									layout : 'column',
									defaultType : 'label',
									items : [{
												xtype:'hidden',
												name:'articleReceive.id',
												id:'articleReceive.id'
											},
											{
												text : '来件单位:',
												width:60
											}, {
												xtype : 'textfield',
												name : 'articleReceive.unit',
												id : 'articleReceive.unit',
												columnWidth : .5,
												maxLength : 64
											}, {
												text : '来文编号:',
												width:60,
												style : 'margin-left:3px;'
											}, {
												xtype : 'textfield',
												name : 'articleReceive.articleNo',
												id : 'articleReceive.articleNo',
												columnWidth : .5,
												maxLength : 64
											}]
								}, {
									xtype : 'container',
									height : 55,
									style:'maring-top:3px;',
									layout : 'column',
									defaultType : 'label',
									items : [{
												text : '主要内容:',
												width:60
											}, {
												xtype : 'textarea',
												name : 'articleReceive.mainContent',
												id : 'articleReceive.mainContent',
												columnWidth : 1,
												height:50
											}]
								}, {
									xtype : 'container',
									height : 25,
									layout : 'column',
									style:'maring-top:3px;',
									defaultType : 'label',
									items : [{
												text : '收文日期:',
												width:60
											}, {
												xtype : 'datefield',
												format : 'Y-m-d',
												name : 'articleReceive.receiveDate',
												id : 'articleReceive.receiveDate',
												columnWidth : .5
											}, {
												text : '完成期限:',
												width:60,
												style : 'margin-left:3px;'
											}, {
												xtype : 'textfield',
												name : 'articleReceive.finishLimit',
												id : 'articleReceive.finishLimit',
												columnWidth : .5,
												maxLength : 64
											}]
								}, {
									xtype : 'container',
									height : 25,
									layout : 'column',
									style:'maring-top:3px;',
									defaultType : 'label',
									items : [{
												text : '收件人:',
												width:60
											}, {
												xtype : 'textfield',
												name : 'articleReceiveUser',
												id : 'articleReceiveUser',
												columnWidth : .5,
												readOnly : true,
												emptyText : '请选择收件人',
												listeners : {
													focus : function() {
														UserSelector.getView(function(ids, names) {
															Ext.getCmp("articleReceiveUser").setValue(names);
															Ext.getCmp("articleReceive.receiveUser.userId").setValue(ids);
														}, true).show();
													}
												}
											}, {
												xtype : 'hidden',
												name : 'articleReceive.receiveUser.userId',
												id : 'articleReceive.receiveUser.userId'
											}, {
												text : '办理人:',
												width:60,
												style : 'margin-left:3px;'
											}, {
												xtype : 'textfield',
												name : 'articleReceiveOperator',
												id : 'articleReceiveOperator',
												columnWidth : .5,
												readOnly : true,
												emptyText : '请选择办理人',
												listeners : {
													focus : function() {
														UserSelector.getView(function(ids, names) {
															Ext.getCmp("articleReceiveOperator").setValue(names);
															Ext.getCmp("articleReceive.operator.userId").setValue(ids);
														}, true).show();
													}
												}
											}, {
												xtype : 'hidden',
												name : 'articleReceive.operator.userId',
												id : 'articleReceive.operator.userId'
											}]
								}]
					}, {
						xtype : 'fieldset',
						title : "发文条目",
						id:'articleRegSendContainer',
						height : 200,
						items : [{
									xtype : 'container',
									height : 25,
									layout : 'column',
									defaultType : 'label',
									items : [{
												xtype:'hidden',
												name:'articleSend.id',
												id:'articleSend.id'
											},{
												text : '收件单位:',
												width:60
											}, {
												xtype : 'textfield',
												name : 'articleSend.unit',
												id : 'articleSend.unit',
												columnWidth : .5,
												allowBlank : false,
												maxLength : 64
											}, {
												text : '发文编号:',
												width:60,
												style : 'margin-left:3px;'
											}, {
												xtype : 'textfield',
												name : 'articleSend.articleNo',
												id : 'articleSend.articleNo',
												columnWidth : .5,
												allowBlank : false,
												maxLength : 64
											}]
								}, {
									xtype : 'container',
									height : 55,
									style:'maring-top:3px;',
									layout : 'column',
									defaultType : 'label',
									items : [{
												text : '主要内容:',
												width:60
											}, {
												xtype : 'textarea',
												name : 'articleSend.mainContent',
												id : 'articleSend.mainContent',
												allowBlank : false,
												columnWidth : 1,
												height:50
											}]
								}, {
									xtype : 'container',
									height : 25,
									layout : 'column',
									style:'maring-top:3px;',
									defaultType : 'label',
									items : [{
												text : '发文日期:',
												width:60
											}, {
												xtype : 'datefield',
												format : 'Y-m-d',
												name : 'articleSend.sendDate',
												id : 'articleSend.sendDate',
												allowBlank : false,
												columnWidth : .5
											}, {
												text : '数量:',
												width:60,
												style : 'margin-left:3px;'
											}, {
												xtype : 'numberfield',
												name : 'articleSend.num',
												id : 'articleSend.num',
												columnWidth : .5,
												allowBlank : false,
												maxLength : 9
											}]
								}, {
									xtype : 'container',
									height : 25,
									layout : 'column',
									style:'maring-top:3px;',
									defaultType : 'label',
									items : [{
												text : '签发人:',
												width:60
											}, {
												xtype : 'textfield',
												name : 'articleSignUser',
												id : 'articleSignUser',
												columnWidth : .5,
												allowBlank : false,
												readOnly : true,
												emptyText : '请选择签发人',
												listeners : {
													focus : function() {
														UserSelector.getView(function(ids, names) {
															Ext.getCmp("articleSignUser").setValue(names);
															Ext.getCmp("articleSend.signUser.userId").setValue(ids);
														}, true).show();
													}
												}
											}, {
												xtype : 'hidden',
												name : 'articleSend.signUser.userId',
												id : 'articleSend.signUser.userId'
											}, {
												text : '收件人:',
												width:60,
												style : 'margin-left:3px;'
											}, {
												xtype : 'textfield',
												name : 'articleSendReceiveUser',
												id : 'articleSendReceiveUser',
												columnWidth : .5,
												allowBlank : false,
												readOnly : true,
												emptyText : '请选择收件人',
												listeners : {
													focus : function() {
														UserSelector.getView(function(ids, names) {
															Ext.getCmp("articleSendReceiveUser").setValue(names);
															Ext.getCmp("articleSend.receiveUser.userId").setValue(ids);
														}, true).show();
													}
												}
											}, {
												xtype : 'hidden',
												name : 'articleSend.receiveUser.userId',
												id : 'articleSend.receiveUser.userId'
											}]
								},{
									xtype : 'container',
									height : 25,
									layout : 'column',
									defaultType : 'label',
									items : [{
												text : '文件存放:',
												width:60
											}, {
												xtype : 'textfield',
												name : 'articleSend.keepAddress',
												id : 'articleSend.keepAddress',
												columnWidth : .5,
												allowBlank : false,
												maxLength : 64
											}, {
												text : '收件日期:',
												width:60,
												style : 'margin-left:3px;'
											}, {
												xtype : 'datefield',
												format : 'Y-m-d',
												name : 'articleSend.receiveDate',
												id : 'articleSend.receiveDate',
												columnWidth : .5,
												allowBlank : false,
												maxLength : 64
											}]
								}]
					}
					]
		});
		//加载表单对应的数据	
		this.initData();
		//初始化功能按钮
		this.buttons = [{
					text : '保存',
					iconCls : 'btn-save',
					handler : this.save.createCallback(this.formPanel, this)
				}, {
					text : '重置',
					iconCls : 'btn-reset',
					handler : this.reset.createCallback(this.formPanel)
				}, {
					text : '取消',
					iconCls : 'btn-cancel',
					handler : this.cancel.createCallback(this)
				}];
	},//end of the initcomponents
	/**
	 * 初始化数据
	 * 
	 * @param {}
	 *            formPanel
	 */
	initData : function() {
		var regId = this.id;
		//加载表单对应的数据	
		if (this.id != null && this.id != 'undefined') {
			this.formPanel.getForm().load({
				deferredRender : false,
				url : __ctxPath + '/produce/getArticleReg.do?id=' + this.id,
				waitMsg : '正在载入数据...',
				success : function(form, action) {
					var jsonData = Ext.util.JSON.decode(action.response.responseText);
					var res = jsonData.data;
					if (!Ext.isEmpty(res.project)) {
						Ext.getCmp("articleRegProName").setValue(res.project.proName);
						Ext.getCmp("articleRegProName").originalValue = res.project.proName;
						Ext.getCmp("articleReg.project.id").setValue(res.project.id);
						Ext.getCmp("articleReg.project.id").originalValue = res.project.id;
					}
					if (!Ext.isEmpty(res.managerUser)) {
						Ext.getCmp("articleRegManagerUser").setValue(res.managerUser.fullname);
						Ext.getCmp("articleRegManagerUser").originalValue = res.managerUser.fullname;
						Ext.getCmp("articleReg.managerUser.userId").setValue(res.managerUser.userId);
						Ext.getCmp("articleReg.managerUser.userId").originalValue = res.managerUser.userId;
					}
					var articleType = res.articleType;
					if(articleType == '收文'){
						Ext.getCmp('articleRegReceiveContainer').show();
						Ext.getCmp('articleRegSendContainer').hide();
						var url = __ctxPath + '/produce/getByRegArticleReceive.do?regId=' + regId;
						var data = ajaxSyncCall(url).data;
						if(!Ext.isEmpty(data)){
							Ext.getCmp('articleReceive.id').setValue(data.id);
							Ext.getCmp('articleReceive.id').originalValue = data.id;
							Ext.getCmp('articleReceive.unit').setValue(data.unit);
							Ext.getCmp('articleReceive.unit').originalValue = data.unit;
							Ext.getCmp('articleReceive.articleNo').setValue(data.articleNo);
							Ext.getCmp('articleReceive.articleNo').originalValue = data.articleNo;
							Ext.getCmp('articleReceive.mainContent').setValue(data.mainContent);
							Ext.getCmp('articleReceive.mainContent').originalValue = data.mainContent;
							Ext.getCmp('articleReceive.receiveDate').setValue(data.receiveDate);
							Ext.getCmp('articleReceive.receiveDate').originalValue = data.receiveDate;
							Ext.getCmp('articleReceive.finishLimit').setValue(data.finishLimit);
							Ext.getCmp('articleReceive.finishLimit').originalValue = data.finishLimit;
							if(!Ext.isEmpty(data.receiveUser)){
								Ext.getCmp('articleReceiveUser').setValue(data.receiveUser.fullname);
								Ext.getCmp('articleReceiveUser').originalValue = data.receiveUser.fullname;
								Ext.getCmp('articleReceive.receiveUser.userId').setValue(data.receiveUser.userId);
								Ext.getCmp('articleReceive.receiveUser.userId').originalValue = data.receiveUser.userId;
							}
							if(!Ext.isEmpty(data.operator)){
								Ext.getCmp('articleReceiveOperator').setValue(data.operator.fullname);
								Ext.getCmp('articleReceiveOperator').originalValue = data.operator.fullname;
								Ext.getCmp('articleReceive.operator.userId').setValue(data.operator.userId);
								Ext.getCmp('articleReceive.operator.userId').originalValue = data.operator.userId;
							}
						}
					}else if(articleType == '发文'){
						Ext.getCmp('articleRegSendContainer').show();
						Ext.getCmp('articleRegReceiveContainer').hide();
						var url = __ctxPath + '/produce/getByRegArticleSend.do?regId=' + regId;
						var data = ajaxSyncCall(url).data;
						if(!Ext.isEmpty(data)){
							Ext.getCmp('articleSend.id').setValue(data.id);
							Ext.getCmp('articleSend.id').originalValue = data.id;
							Ext.getCmp('articleSend.unit').setValue(data.unit);
							Ext.getCmp('articleSend.unit').originalValue = data.unit;
							Ext.getCmp('articleSend.articleNo').setValue(data.articleNo);
							Ext.getCmp('articleSend.articleNo').originalValue = data.articleNo;
							Ext.getCmp('articleSend.mainContent').setValue(data.mainContent);
							Ext.getCmp('articleSend.mainContent').originalValue = data.mainContent;
							Ext.getCmp('articleSend.sendDate').setValue(data.sendDate);
							Ext.getCmp('articleSend.sendDate').originalValue = data.sendDate;
							Ext.getCmp('articleSend.num').setValue(data.num);
							Ext.getCmp('articleSend.num').originalValue = data.num;
							Ext.getCmp('articleSend.keepAddress').setValue(data.keepAddress);
							Ext.getCmp('articleSend.keepAddress').originalValue = data.keepAddress;
							Ext.getCmp('articleSend.receiveDate').setValue(data.receiveDate);
							Ext.getCmp('articleSend.receiveDate').originalValue = data.receiveDate;
							if(!Ext.isEmpty(data.signUser)){
								Ext.getCmp('articleSignUser').setValue(data.signUser.fullname);
								Ext.getCmp('articleSignUser').originalValue = data.signUser.fullname;
								Ext.getCmp('articleSend.signUser.userId').setValue(data.signUser.userId);
								Ext.getCmp('articleSend.signUser.userId').originalValue = data.signUser.userId;
							}
							if(!Ext.isEmpty(data.receiveUser)){
								Ext.getCmp('articleSendReceiveUser').setValue(data.receiveUser.fullname);
								Ext.getCmp('articleSendReceiveUser').originalValue = data.receiveUser.fullname;
								Ext.getCmp('articleSend.receiveUser.userId').setValue(data.receiveUser.userId);
								Ext.getCmp('articleSend.receiveUser.userId').originalValue = data.receiveUser.userId;
							}
						}
					}
					Ext.getCmp('articleType').fireEvent('select',Ext.getCmp('articleType'));
					//Ext.getCmp("").originalValue=
				},
				failure : function(form, action) {
				}
			});
		}
	},
	/**
	 * 重置
	 * 
	 * @param {}
	 *            formPanel
	 */
	reset : function(formPanel) {
		formPanel.getForm().reset();
	},
	/**
	 * 取消
	 * 
	 * @param {}
	 *            window
	 */
	cancel : function(window) {
		window.close();
	},
	/**
	 * 保存记录
	 */
	save : function(formPanel, window) {
		if (formPanel.getForm().isValid()) {
			formPanel.getForm().submit({
				method : 'POST',
				waitMsg : '正在提交数据...',
				success : function(fp, action) {
					Ext.ux.Toast.msg('操作信息', '成功保存信息！');
					var gridPanel = Ext.getCmp('ArticleRegGrid');
					if (gridPanel != null) {
						gridPanel.getStore().reload();
					}
					window.close();
				},
				failure : function(fp, action) {
					Ext.MessageBox.show({
						title : '操作信息',
						msg : '信息保存出错，请联系管理员！',
						buttons : Ext.MessageBox.OK,
						icon : Ext.MessageBox.ERROR
					});
					//window.close();
				}
			});
		}
	}//end of save

});